﻿class BorrowedtablesController < ApplicationController
  # GET /borrowedtables
  # GET /borrowedtables.json
  def index
    @borrowedtables = Borrowedtable.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @borrowedtables }
    end
  end

  def pay
  @borrowedtables = Borrowedtable.find(:all, :conditions => ['NameOfBorrow = ? AND product_id = ? AND PayedDate IS NULL',"#{params[:paybook]}","#{params[:paybook1]}"])

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @borrowedtables }
    end
  end
  
  def ex
    @extension = Borrowedtable.find(:all, :conditions => ['product_id = ? AND NameOfBorrow = ? AND PayedDate IS NULL',"#{params[:ex2]}","#{params[:ex1]}"])
    @ea = params[:ex1]
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @extension,json: @ea }
    end
  end
  
  # GET /borrowedtables/1
  # GET /borrowedtables/1.json
  def show
    @borrowedtable = Borrowedtable.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @borrowedtable }
    end
  end

  # GET /borrowedtables/new
  # GET /borrowedtables/new.json
  def new
    @borrowedtable = Borrowedtable.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @borrowedtable }
    end
  end

  # GET /borrowedtables/1/edit
  def edit
    @borrowedtable = Borrowedtable.find(params[:id])
  end

  # POST /borrowedtables
  # POST /borrowedtables.json
  def create
    @borrowedtable = Borrowedtable.new(params[:borrowedtable])
    @a = 0
    @pro = Product.find(:all, :conditions => ['product_id = ?',"#{@borrowedtable.product_id}"])  
    @pro.each do |pro|
    @a = pro.Status
    pro.update_attribute(:Status,pro.Status = pro.Status - 1)
    @borrowedtable.NameOfDrink = pro.Name
  end
    respond_to do |format|
      if @borrowedtable.save
            format.html { redirect_to @borrowedtable }
            format.json { render json: @borrowedtable, status: :created, location: @borrowedtable }

      else
        format.html { render action: "new" }
        format.json { render json: @borrowedtable.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /borrowedtables/1
  # PUT /borrowedtables/1.json
  def update
    @borrowedtable = Borrowedtable.find(params[:id])

    respond_to do |format|
      if @borrowedtable.update_attributes(params[:borrowedtable])
        format.html { redirect_to @borrowedtable, notice: 'Cập nhật thành công.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @borrowedtable.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /borrowedtables/1
  # DELETE /borrowedtables/1.json
  def destroy
    @borrowedtable = Borrowedtable.find(params[:id])
    @borrowedtable.destroy

    respond_to do |format|
      format.html { redirect_to borrowedtables_url }
      format.json { head :no_content }
    end
  end
  def registration_post
	@aa = "Đăng kí trước thành công"
	RequireBookMail.registration_user_mail(params["body"]).deliver
    respond_to do |format|
      format.html { redirect_to "/borrowedtables/registration", notice: @aa }
      format.json { render json: @aa}
    end
  end
end
